Namespace Utility

    ''' <summary>
    ''' File reader with specified encoding.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class FileReader
        Inherits Object

        Private m_FileName As String
        Private m_throwEx As Boolean
        Private m_encCurrent As Text.Encoding

        ''' <summary>
        ''' Get the current encoding.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CurrentEncoding() As Text.Encoding
            Get
                Return m_encCurrent
            End Get
        End Property

        ''' <summary>
        ''' Get the file comment.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Comment() As String
            Get
                Dim ret As String = String.Empty
                Try
                    Using fs As New IO.FileStream(Me.m_FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
                        Using sr As New IO.StreamReader(fs)
                            ret = sr.ReadToEnd()
                        End Using
                    End Using
                Catch ex As Exception
                    If Me.m_throwEx = True Then
                        Throw ex
                    End If
                End Try
                Return ret
            End Get
        End Property

        ''' <summary>
        ''' Get the file comment array.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Comments() As String()
            Get
                Dim ret As New List(Of String)
                Try
                    Using fs As New IO.FileStream(Me.m_FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
                        Using sr As New IO.StreamReader(fs)
                            While sr.Peek > -1
                                ret.Add(sr.ReadLine)
                            End While
                        End Using
                    End Using
                Catch ex As Exception
                    If Me.m_throwEx = True Then
                        Throw ex
                    End If
                End Try
                Return ret.ToArray
            End Get
        End Property

        ''' <summary>
        ''' Get the encoding from the codepage.
        ''' </summary>
        ''' <param name="CodePage">The codepage of the encoding.</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function GetEncuding(ByVal CodePage As Integer) As Text.Encoding
            Return Text.Encoding.GetEncoding(CodePage)
        End Function

        ''' <summary>
        ''' Create instance.
        ''' </summary>
        ''' <param name="strFileName">The file full name.</param>
        ''' <param name="throwException">Throw exception when error.</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strFileName As String, ByVal throwException As Boolean)
            Me.m_FileName = strFileName
            Me.m_throwEx = throwException
            Me.m_encCurrent = Common.Methods.GetCurrentEncoding
        End Sub

        ''' <summary>
        ''' Create instance.
        ''' </summary>
        ''' <param name="strFileName">The file full name.</param>
        ''' <param name="Encoding">The encoding to load the file.</param>
        ''' <param name="throwException">Throw exception when error.</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strFileName As String, ByVal Encoding As Text.Encoding, ByVal throwException As Boolean)
            Me.m_FileName = strFileName
            Me.m_throwEx = throwException
            Me.m_encCurrent = Encoding
        End Sub

        ''' <summary>
        ''' Create instance.
        ''' </summary>
        ''' <param name="strFileName">The file full name.</param>
        ''' <param name="CodePage">The codepage to load the file.</param>
        ''' <param name="throwException">Throw exception when error.</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strFileName As String, ByVal CodePage As Integer, ByVal throwException As Boolean)
            Me.m_FileName = strFileName
            Me.m_throwEx = throwException
            Me.m_encCurrent = GetEncuding(CodePage)
        End Sub

    End Class

End Namespace
